package observer;

import java.util.ArrayList;
import java.util.List;

public class EnvironmentDataCollector implements Subject {
    private List<Observer> observers;
    private double soilMoisture;
    private int cropGrowthStatus;
    private String weatherData;
    private double soilPH; // 土壤pH值
    private double soilNutrient; // 土壤养分含量
    private double temperature; // 温度
    private double humidity; // 湿度
    private double windSpeed; // 风速

    public EnvironmentDataCollector() {
        observers = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(soilMoisture, cropGrowthStatus, weatherData);
        }
    }

    public void setSoilMoisture(double soilMoisture) {
        this.soilMoisture = soilMoisture;
        System.out.println("土壤湿度更新为: " + soilMoisture + "%");
        notifyObservers();
    }

    public void setCropGrowthStatus(int cropGrowthStatus) {
        this.cropGrowthStatus = cropGrowthStatus;
        System.out.println("作物生长状态更新为: " + cropGrowthStatus + "%");
        notifyObservers();
    }

    public void setWeatherData(String weatherData) {
        this.weatherData = weatherData;
        System.out.println("天气数据更新为: " + weatherData);
        notifyObservers();
    }

    public void setSoilPH(double soilPH) {
        this.soilPH = soilPH;
        System.out.println("土壤pH值更新为: " + soilPH);
        notifyObservers();
    }

    public void setSoilNutrient(double soilNutrient) {
        this.soilNutrient = soilNutrient;
        System.out.println("土壤养分含量更新为: " + soilNutrient + "%");
        notifyObservers();
    }

    public void setTemperature(double temperature) {
        this.temperature = temperature;
        System.out.println("温度更新为: " + temperature + "°C");
        notifyObservers();
    }

    public void setHumidity(double humidity) {
        this.humidity = humidity;
        System.out.println("湿度更新为: " + humidity + "%");
        notifyObservers();
    }

    public void setWindSpeed(double windSpeed) {
        this.windSpeed = windSpeed;
        System.out.println("风速更新为: " + windSpeed + "m/s");
        notifyObservers();
    }
    public double getSoilMoisture() {
        return soilMoisture;
    }
    public int getCropGrowthStatus() {
        return cropGrowthStatus;
    }
    public String getWeatherData() {
        return weatherData;
    }
    public double getSoilPH() {
        return soilPH;
    }
    public double getSoilNutrient() {
        return soilNutrient;
    }
    public double getTemperature() {
        return temperature;
    }
    public double getHumidity() {
        return humidity;
    }
    public double getWindSpeed() {
        return windSpeed;
    }

}